/*
 * Copyright (c) 2010, Michael Hengler (developer (at) hengler (dot) de),
 * Nikolaus Moll (developer (at) miblounge (dot) net).
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the Jombi project nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JOMBI BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.miblounge.jombi.impl.widgets;

import java.util.HashSet;
import java.util.Set;

import net.miblounge.jombi.impl.image.QtImageRegistry;
import net.miblounge.jombi.impl.util.ColorConvert;
import net.miblounge.jombi.impl.util.FontProvider;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.image.IImageConstant;
import org.jowidgets.common.types.Markup;
import org.jowidgets.common.types.Position;
import org.jowidgets.common.widgets.controller.IPopupDetectionListener;
import org.jowidgets.spi.impl.controller.TreeNodeObservable;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.ITreeNodeSpi;
import org.jowidgets.util.Assert;

import com.trolltech.qt.gui.QBrush;
import com.trolltech.qt.gui.QFont;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QTreeWidgetItem;

public class TreeNodeImpl extends TreeNodeObservable implements ITreeNodeSpi {

	private final Set<IPopupDetectionListener> popupDetectionListeners;

	private final TreeImpl parentTree;
	private final QTreeWidgetItem item;

	public TreeNodeImpl(final TreeImpl parentTree, final QTreeWidgetItem parentItem, final Integer index) {
		super();
		Assert.paramNotNull(parentTree, "parentTree");

		this.popupDetectionListeners = new HashSet<IPopupDetectionListener>();

		this.parentTree = parentTree;

		this.item = new QTreeWidgetItem();
		if (index != null) {
			if (parentItem == null) {
				parentTree.getUiReference().insertTopLevelItem(index.intValue(), item);
			} else {
				parentItem.insertChild(index.intValue(), item);
			}
		} else {
			if (parentItem == null) {
				parentTree.getUiReference().addTopLevelItem(item);
			} else {
				parentItem.addChild(item);
			}
		}
	}

	@Override
	public QTreeWidgetItem getUiReference() {
		return item;
	}

	@Override
	public void setEnabled(final boolean enabled) {
		if (!enabled) {
			throw new UnsupportedOperationException("Tree items could not be disabled");
		}
	}

	@Override
	public boolean isEnabled() {
		return true;
	}

	@Override
	public void setText(final String text) {
		if (text != null) {
			getUiReference().setText(0, text);
		} else {
			getUiReference().setText(0, String.valueOf(""));
		}
	}

	@Override
	public void setToolTipText(final String toolTipText) {
		if (toolTipText != null) {
			getUiReference().setToolTip(0, toolTipText);
		} else {
			getUiReference().setToolTip(0, String.valueOf(""));
		}
	}

	@Override
	public void setIcon(final IImageConstant icon) {
		final QIcon oldImage = getUiReference().icon(0);
		final QIcon newImage = QtImageRegistry.getInstance().getImage(icon);
		if (oldImage != newImage) {
			getUiReference().setIcon(0, newImage);
		}
	}

	@Override
	public void setMarkup(final Markup markup) {
		final QFont newFont = FontProvider.deriveFont(item.font(0), markup);
		item.setFont(0, newFont);
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
		item.setForeground(0, new QBrush(ColorConvert.convert(colorValue)));
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
		item.setBackground(0, new QBrush(ColorConvert.convert(colorValue)));
	}

	@Override
	public void setExpanded(final boolean expanded) {
		item.setExpanded(expanded);
		fireExpandedChanged(expanded);
	}

	@Override
	public boolean isExpanded() {
		return item.isExpanded();
	}

	@Override
	public void setSelected(final boolean selected) {
		parentTree.setSelected(this, selected);
	}

	@Override
	public boolean isSelected() {
		return item.isSelected();
	}

	@Override
	public void addPopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionListeners.add(listener);
	}

	@Override
	public void removePopupDetectionListener(final IPopupDetectionListener listener) {
		popupDetectionListeners.remove(listener);
	}

	protected void firePopupDetected(final Position position) {
		for (final IPopupDetectionListener listener : popupDetectionListeners) {
			listener.popupDetected(position);
		}
	}

	@Override
	public ITreeNodeSpi addNode(final Integer index) {
		final TreeNodeImpl result = new TreeNodeImpl(parentTree, item, index);
		parentTree.registerItem(result.getUiReference(), result);
		return result;
	}

	@Override
	public void removeNode(final int index) {
		final QTreeWidgetItem child = getUiReference().takeChild(index);
		if (child != null) {
			parentTree.unRegisterItem(child);
		}
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		return new PopupMenuImpl(parentTree.getUiReference());
	}

}
